package com.sgb.kjwl.view.widget;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.sgb.kjwl.R;
import com.sgb.link.view.widget.XLinearLayoutManager;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.link.view.adapter.PickAdapter;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;

import java.util.ArrayList;
import java.util.List;

public class PickPopWindow2 extends BasePopWindow {

    private PickAdapter pickAdapter1;
    private PickAdapter pickAdapter2;
    private String no;
    private String name;
    private int currentPos;

    public PickPopWindow2(Context context) {
        super(context);
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_filter_packers_2;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        RecyclerView recyclerView1 = holder.getPop().findViewById(R.id.rlv_list);
        RecyclerView recyclerView2 = holder.getPop().findViewById(R.id.rlv2_list);
        TextView reset = holder.getPop().findViewById(R.id.reset);
        TextView confirm = holder.getPop().findViewById(R.id.confirm);
        if (pickAdapter1 == null) {
            pickAdapter1 = new PickAdapter(mContext, null, R.layout.item_pick_filter_child_1);
            pickAdapter1.setItemSelect(true);
            pickAdapter1.setDefItem(0);
        }
        if (pickAdapter2 == null) {
            pickAdapter2 = new PickAdapter(mContext, null, R.layout.item_pick_filter_child_6);
            pickAdapter2.setItemSelect(true);
            pickAdapter2.setDefItem(0);
        }

        XLinearLayoutManager manager = new XLinearLayoutManager(mContext);
        recyclerView1.setLayoutManager(manager);
        recyclerView1.setAdapter(pickAdapter1);
        XLinearLayoutManager manager1 = new XLinearLayoutManager(mContext);
        recyclerView2.setLayoutManager(manager1);
        recyclerView2.setAdapter(pickAdapter2);

        pickAdapter1.setOnItemClick(new AdapterOnItemClick<PickSelectEntity>() {
            @Override
            public void onItemClick(PickSelectEntity pickSelectEntity, int position) {
                setData2(list.get(currentPosition).getChildren().get(position).getChildren());
            }
        });

        pickAdapter2.setOnItemClick(new AdapterOnItemClick<PickSelectEntity>() {
            @Override
            public void onItemClick(PickSelectEntity pickSelectEntity, int position) {
                no = pickSelectEntity.getNo();
                name = pickSelectEntity.getProjectscale();
              /*  if (onResultClick != null) {
                    onResultClick.result(no, name, position);
                }
                dissMiss();*/
            }
        });
        //重置
        reset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onResultClick!=null){
                    onResultClick.reset();
                }
                no = "";
                name = "";
                currentPos = -1;
                setData(currentPosition,list);
                dissMiss();
            }
        });
        //确定
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onResultClick != null) {
                    onResultClick.result(no, name, currentPos);
                    dissMiss();
                }
            }
        });
    }

    private List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity>>>>> list;
    private int currentPosition = 0;

    public void setData(int position, List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity>>>>> list) {
        this.currentPosition = position;
        this.list = list;
        setData1(list.get(position).getChildren());
        setData2(list.get(position).getChildren().get(0).getChildren());
    }

    public void setData1(List<PickSelectEntity<List<PickSelectEntity>>> list) {
        List<PickSelectEntity> pickList = new ArrayList<>();
        for (PickSelectEntity<List<PickSelectEntity>> pickSelectEntity : list) {
            PickSelectEntity entity = new PickSelectEntity();
            entity.setNo(pickSelectEntity.getNo());
            entity.setProjectscale(pickSelectEntity.getProjectscale());
            pickList.add(entity);
        }
        pickAdapter1.setDatas(pickList);
    }

    public void setData2(List<PickSelectEntity> list) {
        if (!list.get(0).getProjectscale().equals("全部")) {
            PickSelectEntity all = new PickSelectEntity();
            all.setNo("");
            all.setProjectscale("全部");
            list.add(0, all);
        }
        pickAdapter2.setDatas(list);
        pickAdapter2.setDefItem(0);
    }

    public void setPosition(int position){
        this.currentPosition = position;
        setData1(list.get(position).getChildren());
        setData2(list.get(position).getChildren().get(0).getChildren());
    }

    private OnResultClick onResultClick;

    public interface OnResultClick {
        void result(Object key1, Object key2, Object key3);

        void reset();
    }

    public void setOnResultClick(OnResultClick onResultClick) {
        this.onResultClick = onResultClick;
    }
}
